package com.base.mail;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarActivity;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.mail.adapter.OrganAdapter;
import com.base.mail.bean.PartListBean;
import com.base.mail.databinding.ActivityOrganBinding;
import com.base.mail.tree.Node;
import com.base.mail.tree.OnTreeNodeClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 组织架构
 * Created by weikailiang on 2019/5/31.
 */
@Route(path = "/mail/organ")
public class OrganActivity extends CBaseBarActivity{
    protected ActivityOrganBinding mBinding;
    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();
    OrganAdapter mAdapter;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_organ;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityOrganBinding) mRootBinding;
    }
    @Override
    protected void start() {
        setBackNavigation();
        setBarTitle("组织架构");
        showPb();
        HashMap<String, String> param = new HashMap<>();
        param.put("type","4");
        Disposable enqueue = RealNetWorkUtil.postContent().method("api-organization/organization").content(param)
                .build().enqueueForArray(PartListBean.class, new CallBackImpl<List<PartListBean>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        hidePb();
                    }

                    @Override
                    public void onNext(List<PartListBean> users) {
                        hidePb();
                        parse(users);
                    }
                });

        mCompositeDisposable.add(enqueue);
    }

    private void parse(List<PartListBean> list){
        final List<Node> mData = new ArrayList<>();
        Node node = null;
        for (PartListBean bean : list){
            node = new Node();
            node.setId(bean.getId());
            node.setpId(bean.getParentId());
            node.setName(bean.getText());
            node.setState(bean.getType()+"");
            if (!TextUtils.isEmpty(bean.getPhone())) {
                node.setPhone(bean.getPhone());
            }
            mData.add(node);
        }
        mBinding.recycler.setLayoutManager(new LinearLayoutManager(this));
        //第一个参数  RecyclerView
        //第二个参数  上下文
        //第三个参数  数据集
        //第四个参数  默认展开层级数 0为不展开
        //第五个参数  展开的图标
        //第六个参数  闭合的图标
        mAdapter = new OrganAdapter(mBinding.recycler,mContext,
                mData, 0);
        mBinding.recycler.setAdapter(mAdapter);
        mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener() {
            @Override
            public void onClick(Node node, int position) {
                if (node.getState().equals("6")){
                    ARouter.getInstance().build("/mail/maildetail")
                            .withString("userId",(String) node.getId()).navigation();
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeDisposable != null&&mCompositeDisposable.isDisposed()){
            mCompositeDisposable.dispose();
        }
    }
}
